{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# BANK JOB\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Ex5%20-%20Bank%20job%20(Solved).ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FEx5%20-%20Bank%20job%20(Solved).ipynb)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You have been hired as a developer in a famous Spanish bank. Your first task as a Python programmer is to develop a program to filter which customers could receive a loan based on their personal information. The requirements are as follows:\n", "\n", "A bank has a CSV file with information about the customers (customers.csv). The file consists of multiple lines, one line for each customer. Each line contains the customer's name, age, nationality, number of children and amount of money in the account. The bank will loan money only to those customers that are younger than 50, have 2 children or more, and have more than 1000 EUR in their account.\n", "\n", "Your task: Design and develop a program in Python that reads the customers' file and prints only the name of those customers that fulfil the conditions to receive a loan.\n", "\n", "Your program must work with files with any number of lines, although you can use the following customer information as a customers.csv example to test your program:\n", "\n", "J. Smith,34,British,0,3040 \n", "D. Bisbal,41,Spanish,2,5100 \n", "R. Nadal,34,Spanish,0,202 \n", "C. Bayo,59,Spanish,1,25 \n", "P. Collins,69,British,2,8392 \n" ] }, { "cell_type": "markdown", "source": [ "## Solution\n", "The figure below illustrates the algorithm used to sort out loans:\n", "\n", "![loans](https://github.com/ffraile/computer_science_tutorials/blob/main/Programming/Extra%20Exercises/img/bank_job.png?raw=true)\n", "\n", "The code below implements the algorithm:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D. Bisbal\n" ] } ], "source": [ "f = open(\"customers.csv\")\n", "line = f.readline()\n", "while line:\n", " fields = line.split(',')\n", " name = fields[0]\n", " age = int(fields[1])\n", " children = int(fields[3])\n", " money = int(fields[4])\n", " if (age < 50) and (children >= 2) and (money > 1000):\n", " print(name)\n", " line = f.readline()\n", " \n", "f.close()" ] }, { "cell_type": "markdown", "source": [], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Analysis questions\n", "\n", "1. **Data Structure:** What is the purpose of the line `fields = line.split(',')`?\n", "\n", "2. **Data output:** How is the list of customers that could receive a loan shown to the user? How could you improve the output?\n", "\n", "3. **Extendability:** If you were to extend the code to capture more details about eligible customers (e.g., their average balance over a year, marital status, etc.), how would you do it? What changes would you need to make to the code?\n", "\n", " 4. **Generalization:** How would you change the code to allow the bank to change the loan conditions (e.g., to increase the maximum age to 60 years old)? What changes would you need to make to the code?\n" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 4 }